package com.think.teaching.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.CourseTeacherMapper;
import com.think.teaching.model.CourseTeacher;
import com.think.teaching.service.CourseTeacherService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程和老师关联表 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class CourseTeacherServiceImpl extends BaseServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

	@Override
	public CourseTeacher AddCourseTeacher(CourseTeacher courseTeacher) {
		
		if (this.save(courseTeacher)) {
			return courseTeacher;
		}
		return null;
	}


	@Override
	public CourseTeacher UpdateCourseTeacher(CourseTeacher courseTeacher) {
		if (this.updateById(courseTeacher)) {
			return courseTeacher;
		}
		return null;
	}

	@Override
	public CourseTeacher GetCourseTeacher(Integer id) {
		QueryWrapper<CourseTeacher> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}
	
	public Boolean CheckHasTeacher(CourseTeacher courseTeacher) {
		QueryWrapper<CourseTeacher> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("teacher_id", courseTeacher.getTeacherId()).eq("course_id", courseTeacher.getCourseId()).ne("delflag", 1);
		return getOne(queryWrapper) != null;
	}

	@Override
	public List<CourseTeacher> SearchCourseTeachersWithKey(String key,Boolean withoutBan) {
		QueryWrapper<CourseTeacher> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("content", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

	@Override
	public IPage<CourseTeacher> ListCourseTeacherWithTagAndType(Integer count, Integer page, Integer type,
			Integer tag,Boolean withoutBan) {
		QueryWrapper<CourseTeacher> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<CourseTeacher> ipage = new Page<>(page, count);
		return this.page(ipage);
	}

	@Override
	public CourseTeacher SetCourseTeacherState(Integer id, Integer state) {
		UpdateWrapper<CourseTeacher> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}
	
	public List<Map<String, Object>> GetTeachersByCourseId(Integer courseId,Boolean withoutBan){
		return baseMapper.GetTeachersByCourseId(courseId,withoutBan);
	}

}
